package ch.dreamweb.readpractice.client.lesetraining.train.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Unit4ViewImpl extends UnitViewImpl implements Unit4View {

	private static Unit4ViewUiBinder uiBinder = GWT
			.create(Unit4ViewUiBinder.class);

	@UiTemplate("Unit4View.ui.xml")
	interface Unit4ViewUiBinder extends UiBinder<Widget, Unit4ViewImpl> {
	}

	protected String loesch;

	protected interface CssStyle extends CssUnitStyle {

		String green();

		String wide();

		String box();

		String colorbox();
	}

	@UiField
	CssStyle style;

	@UiField
	Button button01;
	@UiField
	Button button02;
	@UiField
	Button button03;
	@UiField
	Button button04;
	@UiField
	Button button05;
	@UiField
	Button button06;
	@UiField
	Button button07;
	@UiField
	Button button08;
	@UiField
	Button button09;
	@UiField
	Button button10;
	@UiField
	Button buttonStart;
	@UiField
	TextBox textBoxMorphem;
	@UiField
	Button buttonOk;

	@UiField
	Label labelInfo;

	public Unit4ViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		init();
	}

	@Override
	public void init() {
		super.init();

		// Can access @UiField after calling createAndBindUi
		textBoxMorphem.setText("");
		textBoxMorphem.setMaxLength(20);
		textBoxMorphem.setVisibleLength(10);
		textBoxMorphem.setReadOnly(true);
		textBoxMorphem.setTabIndex(-1);
		textBoxMorphem.setVisible(true);
		buttonOk.setText("OK");
		buttonOk.setTabIndex(1);
		buttonOk.setVisible(false);

		// Grafiken laden, damit keine Lücke entsteht!
		button01.addStyleName(style.colorbox());
		button01.removeStyleName(style.colorbox());
	}

	// Add a UI Factory method for the sub-widget & pass object

	// @Override
	// public void setPresenter(Presenter presenter) {
	// super.setPresenter(presenter);
	// // three.setEventBus(presenter.getEventBus());
	// }

	@UiHandler("buttonStart")
	void onButtonStartClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onStartButtonClicked();
		}
	}

	@UiHandler("buttonOk")
	void onButtonOkClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onOkButtonClicked();
		}

	}

	@UiHandler("button01")
	void onButton01Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(0);
		}
	}

	@UiHandler("button02")
	void onButton02Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(1);
		}
	}

	@UiHandler("button03")
	void onButton03Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(2);
		}
	}

	@UiHandler("button04")
	void onButton04Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(3);
		}
	}

	@UiHandler("button05")
	void onButton05Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(4);
		}
	}

	@UiHandler("button06")
	void onButton06Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(5);
		}
	}

	@UiHandler("button07")
	void onButton07Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(6);
		}
	}

	@UiHandler("button08")
	void onButton08Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(7);
		}
	}

	@UiHandler("button09")
	void onButton09Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(8);
		}
	}

	@UiHandler("button10")
	void onButton10Click(ClickEvent e) {
		if (presenter != null) {
			((Unit4View.Unit4Presenter) presenter).onAnswerButtonClicked(9);
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setMorphem(String morphem) {
		textBoxMorphem.setText(morphem);
	}

	@Override
	public void setOkEnabled(boolean enabled) {
		buttonOk.setEnabled(enabled);
		buttonOk.setFocus(true);
	}

	@Override
	public void setStartEnabled(boolean enabled) {
		buttonStart.setEnabled(enabled);
	}

	@Override
	public void setFocusStart() {
		buttonStart.setFocus(true);
	}

	@Override
	public void changeColor(int buttonNumber) {
		switch (buttonNumber) {
		case 0: {
			button01.addStyleName(style.colorbox());

			break;
		}
		case 1: {
			button02.addStyleName(style.colorbox());

			break;
		}
		case 2: {
			button03.addStyleName(style.colorbox());
			;
			break;
		}
		case 3: {
			button04.addStyleName(style.colorbox());

			break;
		}
		case 4: {
			button05.addStyleName(style.colorbox());
			break;
		}
		case 5: {
			button06.addStyleName(style.colorbox());
			break;
		}
		case 6: {
			button07.addStyleName(style.colorbox());
			break;
		}
		case 7: {
			button08.addStyleName(style.colorbox());
			break;
		}
		case 8: {
			button09.addStyleName(style.colorbox());
			break;
		}
		case 9: {
			button10.addStyleName(style.colorbox());
			break;
		}
		}
	}

	@Override
	public void resetColor() {
		button01.removeStyleName(style.colorbox());
		button02.removeStyleName(style.colorbox());
		button03.removeStyleName(style.colorbox());
		button04.removeStyleName(style.colorbox());
		button05.removeStyleName(style.colorbox());
		button06.removeStyleName(style.colorbox());
		button07.removeStyleName(style.colorbox());
		button08.removeStyleName(style.colorbox());
		button09.removeStyleName(style.colorbox());
		button10.removeStyleName(style.colorbox());
	}

	@Override
	public void setButtons(String[] all) {
		button01.setText(all[0]);
		button02.setText(all[1]);
		button03.setText(all[2]);
		button04.setText(all[3]);
		button05.setText(all[4]);
		button06.setText(all[5]);
		button07.setText(all[6]);
		button08.setText(all[7]);
		button09.setText(all[8]);
		button10.setText(all[9]);
	}

	@Override
	public Label getLabelInfo() {
		return labelInfo;
	}

	@Override
	public CssUnitStyle getStyle() {
		return style;
	}

	@Override
	public Button getButtonStart() {
		return buttonStart;
	}

	@Override
	public void setButtonsEnabled(Boolean enabled) {
		button01.setEnabled(enabled);
		button02.setEnabled(enabled);
		button03.setEnabled(enabled);
		button04.setEnabled(enabled);
		button05.setEnabled(enabled);
		button06.setEnabled(enabled);
		button07.setEnabled(enabled);
		button08.setEnabled(enabled);
		button09.setEnabled(enabled);
		button10.setEnabled(enabled);
	}

}
